package cn.yunyichina.provider.report.service.impl;

import cn.yunyichina.provider.framework.common.ResponseEnum;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.report.dao.RegisterMapper;
import cn.yunyichina.provider.report.dao.SummaryMapper;
import cn.yunyichina.provider.report.domain.data.RegisterDO;
import cn.yunyichina.provider.report.domain.data.SummaryDO;
import cn.yunyichina.provider.report.domain.vo.BaseBusinessRequestVO;
import cn.yunyichina.provider.report.domain.vo.BaseRequestVO;
import cn.yunyichina.provider.report.service.BaseService;
import cn.yunyichina.provider.report.service.SummaryService;
import cn.yunyichina.utils.json.Jackson;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.List;
import java.util.Set;

/**
 * 汇总统计
 * Created by Lullaby on 2017/12/12
 */
@Service
public class SummaryServiceImpl extends BaseService implements SummaryService {

    private static final Logger logger = LoggerFactory.getLogger(SummaryServiceImpl.class);

    @Autowired
    private Validator validator;

    @Autowired
    private SummaryMapper summaryMapper;

    /**
     * 统计汇总，包括：挂号书、绑卡数、支付笔数
     *
     * @param json 入参
     */
    public ResponseObject countSummary(String json) {
        logger.info("SummaryServiceImpl.countSummary.json -> {}", json);

        BaseRequestVO requestVO = Jackson.fromJson(json, BaseRequestVO.class);

        Set<ConstraintViolation<BaseRequestVO>> constraints = validator.validate(requestVO);
        if (!constraints.isEmpty()) {
            List<String> messages = getConstraintMessageList(constraints);
            return new ResponseObject(ResponseEnum.Common.INVALID.getCode(), ResponseEnum.Common.INVALID.getMessage(), messages);
        }

        PageHelper.startPage(requestVO.getPageNum(), requestVO.getPageSize());
        List<SummaryDO> list = summaryMapper.list(null);

        return new ResponseObject().buildResultObject(new PageInfo<>(list));
    }

}
